﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YunSpace.Application.SystemManage;
using YunSpace.Core;
using YunSpace.Data;
using YunSpace.Domain.Entity.MiniSite;
using YunSpace.Domain.Entity.SiteMgr;
using YunSpace.Domain.Entity.SystemManage;
using YunSpace.Domain.IRepository.SiteMgr;
using YunSpace.Domain.ViewModel.SiteMgr;
using YunSpace.Repository.SiteMgr;

namespace YunSpace.Application.SiteMgr
{
    public class ClientTrackingApp
    {
        ISiteClientTrackingRepository trackingService = new SiteClientTrackingRepository();
        ISiteClientsRepository clientsService = new SiteClientsRepository();
        ISiteClientVisitRepository visitService = new SiteClientVisitRepository();

        private ItemsDetailApp itemsDetailApp = new ItemsDetailApp();
        public List<SiteClientTrackingActionModel> GetList(Pagination pagination, string queryJson,string CustomerId) {
            var result = new List<SiteClientTrackingActionModel>();
            using (var db = new RepositoryBase().BeginTrans()) {
                var strSql = @"select a.F_Id,b.F_ClientName,a.F_ClientId,a.F_ActivityName,a.F_CityId,a.F_ActivityIndustry,a.F_ActivityType,a.F_BudgetCash,a.F_ActivityStep
                            ,a.F_Brand,a.F_SiteType,a.F_SiteSize,a.F_PeopleNum,a.F_Companions,a.F_CreatorTime,a.F_DeleteMark,a.F_CreatorUserId,
                            a.F_LastModifyTime,a.F_LastModifyUserId,a.F_DeleteTime,a.F_DeleteUserId,a.F_InStartTime,a.F_StartTime,a.F_EndTime from Site_ClientTracking a 
                            inner join Site_Clients b on a.F_ClientId=b.F_Id where 1=1 {0} order by a.F_CreatorTime desc";               
                var queryParam = queryJson.ToJObject();
                DbParameter[] parameter =
                {
                    new MySqlParameter("@F_CreatorUserId", queryParam["F_CreatorUserId"]),
                };
                strSql = string.Format(strSql, CustomerId.IsEmpty() ? "" : "and a.F_CreatorUserId='"+ CustomerId + "'");
                result = db.FindList<SiteClientTrackingActionModel>(strSql, parameter, pagination);
            }
            return result;
        }

        public List<SiteClientTrackingActionModel> GetListFor(Pagination pagination, string queryJson)
        {
            var result = new List<SiteClientTrackingActionModel>();
            using (var db = new RepositoryBase().BeginTrans())
            {
                var strSql = @"select a.F_Id,b.F_ClientName,a.F_ClientId,a.F_ActivityName,a.F_CityId,a.F_ActivityIndustry,a.F_ActivityType,a.F_BudgetCash,a.F_ActivityStep
                            ,a.F_Brand,a.F_SiteType,a.F_SiteSize,a.F_PeopleNum,a.F_Companions,a.F_CreatorTime,a.F_DeleteMark,c.F_RealName F_CreatorUserId,
                            a.F_LastModifyTime,a.F_LastModifyUserId,a.F_DeleteTime,a.F_DeleteUserId,a.F_InStartTime,a.F_StartTime,a.F_EndTime from Site_ClientTracking a 
                            inner join Site_Clients b on a.F_ClientId=b.F_Id 
                            inner join Sys_User c on a.F_CreatorUserId=c.F_Id where 1=1 {0} {1} order by a.F_CreatorTime desc";
                var queryParam = queryJson.ToJObject();
                DbParameter[] parameter =
                {
                    new MySqlParameter("@F_CreatorUserId", queryParam["UserId"]),
                    new MySqlParameter("@F_ClientId", queryParam["F_ClientId"])
                };
                strSql = string.Format(strSql, queryParam["UserId"].IsEmpty() ? "" : "and a.F_CreatorUserId=@F_CreatorUserId", queryParam["F_ClientId"].IsEmpty() ? "" : "and a.F_ClientId=@F_ClientId");
                result = db.FindList<SiteClientTrackingActionModel>(strSql, parameter, pagination);
            }
            return result;
        }        

        public List<SiteClientVisitEntity> GetDetailListFor(Pagination pagination, string queryJson)
        {
            var result = new List<SiteClientVisitEntity>();
            using (var db = new RepositoryBase().BeginTrans())
            {
                var strSql = @"select a.F_Id,b.F_ClientName F_ClientId,a.F_DockingName,a.F_Phone,a.F_PositionId,a.F_Target,a.F_Action,a.F_ActionDate,
                                a.F_Progress,a.F_IndexRemark,a.F_DeleteMark,a.F_CreatorTime,a.F_CreatorUserId,a.F_LastModifyTime,a.F_LastModifyUserId,
                                a.F_DeleteTime,a.F_DeleteUserId from Site_ClientVisit a 
                                inner join Site_Clients b on a.F_ClientId=b.F_Id where 1=1 {0} order by a.F_CreatorTime desc";
                var queryParam = queryJson.ToJObject();
                DbParameter[] parameter =
                {                   
                    new MySqlParameter("@F_ClientId",queryParam["F_ClientId"])
                };
                strSql = string.Format(strSql, " and a.F_ClientId=@F_ClientId");
                result = db.FindList<SiteClientVisitEntity>(strSql, parameter, pagination);
            }
            return result;
        }

        public List<SiteClientVisitEntity> GetDetailList(Pagination pagination, string queryJson, string CustomerId) {
            var result = new List<SiteClientVisitEntity>();
            using (var db = new RepositoryBase().BeginTrans()) {
                var strSql = @"select a.F_Id,b.F_ClientName F_ClientId,a.F_DockingName,a.F_Phone,a.F_PositionId,a.F_Target,a.F_Action,a.F_ActionDate,
                                a.F_Progress,a.F_IndexRemark,a.F_DeleteMark,a.F_CreatorTime,a.F_CreatorUserId,a.F_LastModifyTime,a.F_LastModifyUserId,
                                a.F_DeleteTime,a.F_DeleteUserId from Site_ClientVisit a 
                                inner join Site_Clients b on a.F_ClientId=b.F_Id where 1=1 {0} {1} order by a.F_CreatorTime desc";
                var queryParam = queryJson.ToJObject();
                DbParameter[] parameter =
                {
                    new MySqlParameter("@F_CreatorUserId", CustomerId),
                    new MySqlParameter("@F_ClientId",queryParam["F_ClientId"])
                };
                strSql = string.Format(strSql, CustomerId.IsEmpty() ? "" : "and a.F_CreatorUserId=@F_CreatorUserId", " and a.F_ClientId=@F_ClientId");
                result = db.FindList<SiteClientVisitEntity>(strSql, parameter, pagination);
            }
            return result;
        }

        public SiteClientTrackingActionModel GetEntity(string keyValue) {
            var result =new SiteClientTrackingActionModel();
            using (var db = new RepositoryBase().BeginTrans())
            {
                var strSql = @"select a.F_Id,b.F_ClientName,a.F_ClientId,a.F_ActivityName,a.F_CityId,a.F_ActivityIndustry,a.F_ActivityType,a.F_BudgetCash,a.F_ActivityStep
                            ,a.F_Brand,a.F_SiteType,a.F_SiteSize,a.F_PeopleNum,a.F_Companions,a.F_CreatorTime,a.F_DeleteMark,a.F_CreatorUserId,
                            a.F_LastModifyTime,a.F_LastModifyUserId,a.F_DeleteTime,a.F_DeleteUserId,a.F_InStartTime,a.F_StartTime,a.F_EndTime from Site_ClientTracking a 
                            inner join Site_Clients b on a.F_ClientId=b.F_Id where 1=1 {0} order by F_CreatorTime desc";                
                DbParameter[] parameter =
                {                   
                    new MySqlParameter("@F_Id",keyValue)
                };
                strSql = string.Format(strSql, keyValue.IsEmpty() ? "" : "and a.F_Id=@F_Id");
                result = db.FindList<SiteClientTrackingActionModel>(strSql, parameter).FirstOrDefault();
            }
            return result;
        }

        public SiteClientVisitEntity GetDetailEntity(string keyValue)
        {
            return visitService.FindEntity(keyValue);
        }

        public List<ItemsDetailActionModel> GetItemList(string enCode)
        {
            return itemsDetailApp.GetItemList(enCode);
        }

        public List<SiteClientActionModel> GetClientsList() {
            var result = new List<SiteClientActionModel>();
            using (var db = new RepositoryBase().BeginTrans()) {
                var strSql = @"select F_Id,F_ClientName from Site_Clients order by F_CreatorTime desc";
                result = db.FindList<SiteClientActionModel>(strSql);
            }
            return result;
        }

        public void SubmitForm(SiteClientTrackingEntity clientEntity, string keyValue) {
            if (!string.IsNullOrEmpty(keyValue))
            {
                clientEntity.Modify(keyValue);               
                trackingService.Update(clientEntity);
            }
            else
            {
                clientEntity.Create();
                trackingService.Insert(clientEntity);
            }
        }

        public void SubmitVisitForm(SiteClientVisitEntity visitEntity,string F_ClientId, string keyValue) {
            if (!string.IsNullOrEmpty(keyValue))
            {
                visitEntity.Modify(keyValue);
                visitService.Update(visitEntity);
            }
            else {
                visitEntity.Create();
                visitEntity.F_ClientId = F_ClientId;
                visitService.Insert(visitEntity);
            }
        }

        public void DeleteForm(string keyValue) {
            SiteClientVisitEntity visitEntity = visitService.FindEntity(keyValue);
            visitService.Delete(visitEntity);
        }

        public void Delete(string keyValue)
        {
            SiteClientTrackingEntity trackEntity = trackingService.FindEntity(keyValue);
            trackingService.Delete(trackEntity);
        }
    }
}
